package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.dao.TestrecordDao;
import com.example.yuntiku.entity.Exam;
import com.example.yuntiku.entity.Testrecord;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestrecordDaoImpl extends BaseDao implements TestrecordDao {
    public int insertTestrecord(Testrecord testrecord){
        String sql = "INSERT INTO `yuntiku`.`testrecord` (`sid`,`examid`,`starttime`,`submit`,`answersum`,`score`,`terrace`,`pattern`,`correct`) \n" +
                "VALUES(?,?,?,?,?,?,?,?,?);";
        return super.insertForId(sql,
                testrecord.getSid(),
                testrecord.getExamid(),
                testrecord.getStarttime(),
                testrecord.getSubmit(),
                testrecord.getAnswersum(),testrecord.getScore(),testrecord.getTerrace(),testrecord.getPattern(),testrecord.getCorrect());
    }

    @Override
    public Testrecord selectByUserIdAndExamId(int userId, int examId) {
        String sql="SELECT `id`,`sid`,`examid`,`starttime`,`submit`,`answersum`,`score`,`terrace`,`pattern`,`correct`\n" +
                "FROM `testrecord`\n" +
                "WHERE `sid`=? AND `examid`=?;";
        ResultSet rs=null;
        try {
            rs=super.executeQuery(sql,userId,examId);
            while (rs.next()) { //一次读取一行记录，读完返回false
                //转换成实体类对象，一个对象，装一条数据
                Testrecord entity = new Testrecord();
                entity.setId(rs.getInt("id"));
                entity.setSid(rs.getInt("sid"));
                entity.setExamid(rs.getInt("examid"));
                entity.setStarttime(rs.getString("starttime"));
                entity.setSubmit(rs.getString("submit"));
                entity.setAnswersum(rs.getInt("answersum"));
                entity.setScore(rs.getInt("score"));
                entity.setTerrace(rs.getInt("terrace"));
                entity.setPattern(rs.getInt("pattern"));
                entity.setCorrect(rs.getInt("correct"));
                //添加到集合
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,null,rs);
        }
        return null;
    }

    @Override
    public int update(Testrecord testrecord) {
        String sql="UPDATE `testrecord`\n" +
                "SET `starttime`=?,`submit`=?,`answersum`=?,`score`=?,`terrace`=?,`pattern`=?,`correct`=?\n" +
                "WHERE `sid`=? AND `examid`=?;";
        return executeUpdate(sql,
                testrecord.getStarttime(),
                testrecord.getSubmit(),
                testrecord.getAnswersum(),
                testrecord.getScore(),
                testrecord.getTerrace(),
                testrecord.getPattern(),
                testrecord.getCorrect(),
                testrecord.getSid(),
                testrecord.getExamid());
    }

    public List<Testrecord> Studentexam(int exam){
        List<Testrecord> list=new ArrayList<>();
        String sql="SELECT `examid`\n" +
                "FROM `testrecord` t\n" +
                "JOIN `exam` e ON e.`id`=t.`examid`\n" +
                "WHERE e.`terminaltime`>NOW() AND `sid`=?";
        ResultSet rs=null;
        try {
            rs=super.executeQuery(sql);
            while (rs.next()) { //一次读取一行记录，读完返回false
                //转换成实体类对象，一个对象，装一条数据
                Testrecord entity = new Testrecord();
                entity.setId(rs.getInt("examid"));
                //添加到集合
                list.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,null,rs);
        }
        return list;
    }
    public int updateTestrecord(int correct,int id){
         String sql = "UPDATE `yuntiku`.`testrecord` SET `correct` = ? WHERE `id` = ?;";
         return super.executeUpdate(sql,correct,id);
    }
}
